/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 05.08.2006
 *
 *************************************************/
package fightplan.business.util;

public class BeltUtil {

	public static final int MIN_BELT = -9;
	public static final int MAX_BELT = 9;
	public static final String ALL_BELT = "Alle";
	public static final String FORMAT_WARNING = "Die Gurtstufe muss im Format \"3k\" oder \"3d\" angegeben werden.\nDer Bereich ist eingeschränkt von 9k bis 9d.";

	public static int parse(String input) throws IllegalArgumentException {
		if (input == null)
			throw new IllegalStateException();

		// Den String zuerst säubern, z.B "1.Kyu" -> "1k"
		String s = "";
		for (int i = 0; i < input.length(); i++) {
			char c = input.charAt(i);
			if (Character.isDigit(c) && c != '0') {
				s += input.charAt(i);
			} else if (c == 'k' || c == 'K') {
				s += "k";
			} else if (c == 'd' || c == 'D') {
				s += "d";
			}
		}

		if (s.length() != 2)
			throw new IllegalArgumentException(FORMAT_WARNING);
		if (!Character.isDigit(s.charAt(0)))
			throw new IllegalArgumentException(FORMAT_WARNING);
		char c = s.charAt(1);
		if (c == 'd') {
			return Integer.parseInt(s.substring(0, 1));
		} else if (c == 'k') {
			return -Integer.parseInt(s.substring(0, 1));
		} else {
			throw new IllegalArgumentException(FORMAT_WARNING);
		}
	}

	public static String format(int belt) {
		if (belt < 0) {
			return (-belt) + "k";
		} else {
			return belt + "d";
		}
	}

	public static int parseRangeStart(String s) {
		if (s == null)
			throw new IllegalStateException();
		s = s.trim();
		if (ALL_BELT.equals(s))
			return MIN_BELT;
		int pos = s.indexOf("-");
		if (pos == 0)
			return MIN_BELT;
		if (pos == -1)
			return parse(s);
		return parse(s.substring(0, pos));
	}

	public static int parseRangeEnd(String s) {
		if (s == null)
			throw new IllegalStateException();
		s = s.trim();
		if (ALL_BELT.equals(s))
			return MAX_BELT;
		int pos = s.indexOf("-");
		if (pos == s.length() - 1)
			return MAX_BELT;
		if (pos == 0)
			return parse(s.substring(1));
		return parse(s.substring(pos + 1, s.length()));
	}

	public static String formatRange(int start, int end) {
		if (start <= MIN_BELT && end >= MAX_BELT) {
			return "Alle";
		} else if (start == end) {
			return "" + format(start);
		} else {
			return format(start) + " - " + format(end);
		}
	}

	public static String formatRangeForDescription(int start, int end) {
		if (start <= MIN_BELT) {
			if (end >= MAX_BELT) {
				return "";
			} else {
				return "bis " + formatForDescription(end, true);
			}
		} else {
			if (end >= MAX_BELT) {
				return "ab " + formatForDescription(start, true);
			} else if (start == end) {
				return formatForDescription(start, true);
			} else {
				return formatForDescription(start, false) + "-" + formatForDescription(end, true);
			}
		}
	}

	private static String formatForDescription(int belt, boolean with) {
		if (belt < 0) {
			return "" + (-belt) + (with ? "kyu" : "");
		} else {
			return "" + belt + (with ? "dan" : "");
		}
	}

}